package de.mrclrchtr.education.account.controller

import de.mrclrchtr.education.account.dto.RoleDto
import de.mrclrchtr.education.account.jooq.tables.pojos.Role
import de.mrclrchtr.education.account.jooq.tables.pojos.User
import de.mrclrchtr.education.account.service.RoleService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/role")
class RoleController(@Autowired private val roleService: RoleService) {

    data class RoleUpdateRequest(val role: Role, val userIds: List<Int>)

    /**
     * 获取所有的角色
     */
    @GetMapping("")
    fun getAllRoles(): MutableList<RoleDto> {
        return roleService.getAllRoles()
    }

    /**
     * 根据id获取角色
     */
    @GetMapping("/{id}")
    fun getRoleByIdWithUsers(@PathVariable id: Int?): RoleDto? {
        return roleService.getRoleByIdWithUsers(id)
    }

    /**
     * 修改角色,基础信息加上更新的userid
     */
    @PutMapping
    fun updateRole(@RequestBody roleRequest: RoleUpdateRequest): String {
        return roleService.updateRole(roleRequest.role, roleRequest.userIds)
    }
    /**
     * 新增角色
     */
    @PostMapping
    fun addRole(@RequestBody role: Role): String {
//        println(role)
        return roleService.addRole(role)
    }

    /**
     * 根据id删除角色
     */
    @DeleteMapping("{id}")
    fun deleteRoleById(@PathVariable id: Int?): String {
        return roleService.deleteRoleById(id)
    }
}